package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.insurance;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @date 2020/12/22 17:30
 */
public class Llt030InsuranceStrategy implements InsuranceStrategy{
    @Override
    public InsuredModel checkInsuredRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, ProductConfig productConfig) {
        //被保人信息
        CustInfoModel custInfoModel = acceptRequestDataModel.getRequest().getCustInfo();
        List<InsurantModel> insurantModelList = custInfoModel.getInsurants().getInsurant();
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        Date cValiDate = requestModel.getPolicyInfo().getCvalidate();
        AppntModel appntModel = requestModel.getCustInfo().getAppnt();
        InsurantModel insurantModel = AcceptUtil.getInsurantModelBySeq(productModel.getProdInsSeq(), insurantModelList);
        int insurantYear = DateUtil.getYears(insurantModel.getBirthday(), cValiDate);
        //被保险人：18-60周岁，支持为本人、子女、父母、配偶投保
        String relation = insurantModel.getRelToApp();
        if (insurantYear < UnderWriteConstant.AGE_EIGHTEEN_YEAR || insurantYear > UnderWriteConstant.AGE_SIXTY_YEAR) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    "被保人年龄必须为18-60周岁");
        }
        //投保人与被保人的关系：本人、配偶、父母和子女
        if (!(productConfig.getRelationList().contains(relation))) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                    UnderWriteConstant.UNDER_WRITE_NUGG0007_MESSAGE);
        }
        //既往重疾险理赔记录
        if (StringUtils.equals(AcceptConstant.CREDENTIALS_IDNO, insurantModel.getIdType())) {
            if (insuredCoreRepository.historicalMajorDiseases(insurantModel.getIdNo(), AcceptConstant.CREDENTIALS_IDNO) > 0) {
                return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE,
                        String.format(UnderWriteConstant.UNDER_WRITE_NUGG0060_MESSAGE, insurantModel.getName()));
            }
        }
        /*//被保人健康告知
        if (StringUtils.equals(AcceptConstant.STRING_Y, insurantModel.getInsInform())) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_UNDEFINED,
                    String.format(UnderWriteConstant.UNDER_WRITE_INS_INFORM, insurantModel.getName()));
        }*/
        //黑名单
        if (insurantModel.getBlackListFlag() > 0) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_NUGG0031,
                    String.format(UnderWriteConstant.UNDER_WRITE_NUGG0031_MESSAGE, insurantModel.getName()));
        }
        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE,
                AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }
}
